﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Net.Class;
using System.Text.RegularExpressions;

namespace Net
{
    public partial class FormAddMoney : Form
    {
        public FormAddMoney()
        {
            InitializeComponent();
        }

        public static FormAddMoney childform = null;
        public static FormAddMoney signleton()
        {
            if (childform == null)
            {
                childform = new FormAddMoney();
            }
            return childform;
        }

        private void checkBox_ifCurrentUser_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox_ifCurrentUser.Checked)
            {
                this.textBox_userName.Text = NetCommon.userName;
                this.textBox_password.Text = NetCommon.passWord;
                textBox_userName.Enabled = false;
                textBox_password.Enabled = false;
            }
            else
            {
                textBox_userName.Enabled = true;
                textBox_password.Enabled = true;
            }
        }

        private void button_cancelAddMoney_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormAddMoney_Load(object sender, EventArgs e)
        {
            checkBox_ifCurrentUser_CheckedChanged(sender, e);
            button_addMoney.Enabled = false;
        }

        private void button_addMoney_Click(object sender, EventArgs e)
        {
            string username = textBox_userName.Text.Trim();
            string password = textBox_password.Text.Trim();
            string moneyCardId = textBox_moneyCardId.Text;
            if ("".Equals(username) || "".Equals(password) || "".Equals(moneyCardId))
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "信息不完整 ", "您填写的信息不完整,请检查", ToolTipIcon.Info);
                return;
            }
            //else if (moneyCardId.Length!=20)
            //{
            //    MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "信息错误 ", "您的充值码长度应该是20位", ToolTipIcon.Info);
            //    return;
            //}
            string postUrl = "http://202.202.96.9/account/user_cz1.jsp";
            string postData = "username=" + username + "&password=" + password + "&card_no=" + moneyCardId;
            string returnContent = NetCommon.striphtml(GetPage.getPage(postUrl,postData));
            if (returnContent == null || returnContent.Equals(""))
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "充值失败 ", "无响应，请检查网络……", ToolTipIcon.Info);
                return;
            }
            string tipString = "";
            MainForm.mainform.richTextBox1.Text = returnContent;
            tipString = NetCommon.getSubStringToEnd(returnContent, "网络使用费充值");
            tipString = NetCommon.removeBlankLines(tipString);//去掉空行
            MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "充值结果：", tipString, ToolTipIcon.Info);
            Regex r = new Regex("返回重新输入");
            Match m = r.Match(returnContent);
            if (!m.Success) //如果没有叫返回重新输入，则表示充值成功了，关闭当前窗口。还没有测试过……
            {
                this.Close();
            }
            
        }

        private void textBox_moneyCardId_TextChanged(object sender, EventArgs e)
        {
            string username = textBox_userName.Text.Trim();
            string password = textBox_password.Text.Trim();
            string moneyCardId = textBox_moneyCardId.Text;
            if ("".Equals(username) || "".Equals(password) || !(moneyCardId.Length == 20))
            {
                button_addMoney.Enabled = false;
                return;
            }
            else if (moneyCardId.Length == 20)
            {
                button_addMoney.Enabled = true;
            }
           
        }

        private void formClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.Dispose(true);
                childform = null;
            }
            catch (Exception ex)
            { MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "出现异常 ", ex.ToString(), ToolTipIcon.Info); }
        }

       
    }
}
